var searchByName = [
    { old: "ESG Risk Rating Percentile - Subindustry", new: "ESG Risk Percentile-Subindustry"                 },
    { old: "Corporate Governance - ESG Risk Rating",   new: "Issue - Corporate Governance-Risk Score"         },
    { old: "Corporate Governance-Weight",              new: "Issue - Corporate Governance-Risk Contribution"  },
    { old: "Corporate Governance-ESG Risk Category",   new: "Issue - Corporate Governance-Risk Category"      },
    { old: "Corporate Governance-Managed Risk",        new: "Issue - Corporate Governance-Managed Risk Score" },
    { old: /-ESG Risk Category$/,                      new: "-Risk Category"                                  },
    { old: / - Event Name$/,                           new: "-Event Name"                                     },
    { old: / - Event Category$/,                       new: "-Event Category"                                 },
    { old: / - ESG Risk Rating$/,                      new: "-Risk Score"                                     },
    { old: / - Weight$/,                               new: "-Risk Contribution"                              },
    { old: / - Exposure Score$/,                       new: "-Exposure Score"                                 },
    { old: / - Managed Risk$/,                         new: "-Managed Risk Score"                             },
]

var searchByKey = [
    { old: "ESG Risk Rating",                        new: "ESG Risk Score"                          },
    { old: "ESG Risk Rating Rank",                   new: "ESG Risk Rank-Universe"                  },
    { old: "ESG Risk Rating Percentile",             new: "ESG Risk Percentile-Universe"            },
    { old: "ESG Risk Rating Percentile-Subindustry", new: "ESG Risk Percentile-Subindustry"         },
    { old: "ESG Management Score",                   new: "Overall Management Score"                },
    { old: "ESG Exposure Score",                     new: "Overall Exposure Score"                  },
    { old: "Managed Risk",                           new: "Overall Managed Risk Score"              },
    { old: "E128 Forest Certifications",             new: "E128 Forest Certifications-Raw Score-RR" },
    { old: /-ESG Risk Rating$/,                      new: "-Risk Score"                             },
    { old: /-Managed Risk$/,                         new: "-Managed Risk Score"                     },
    { old: /-Subindustry Exposure Score$/,           new: "-Default Exposure Score"                 },
    { old: /-Issue Beta$/,                           new: "-Beta"                                   },
    { old: /-Weight$/,                               new: "-Risk Contribution"                      },
    { old: /-Raw Score$/,                            new: "-Raw Score-RR"                           },
]

var updateBy = field => change => {
    var filter = { ResearchProducts: { $in: [ "RiskRating", "CoreRiskRating" ] } }
    filter[field] = change.old
    
    var updatetemplate = x => {
        var newName = x[field].replace(change.old, change.new)
        x.Name = newName
            .replace(/-RR$/, "")
            .replace("Issue - Corporate Governance", "Corporate Governance")
        x.CriteriaEntityField = newName.replace(/\./g, "")
        db.TemplateCriteria.save(x)
    }
	
	var updatefilter = x => {
        var newName = x[field].replace(change.old, change.new)
        x.Name = newName
            .replace(/-RR$/, "")
            .replace("Issue - Corporate Governance", "Corporate Governance")
        x.CriteriaEntityField = newName.replace(/\./g, "")
		x.SelectedValueInformation = newName.replace(/\./g, "")
        db.FilterCriteria.save(x)
    }
    
    db.TemplateCriteria.find(filter).forEach(updatetemplate)
    db.FilterCriteria.find(filter).forEach(updatefilter)
}

searchByName.forEach(updateBy("Name"))
searchByKey.forEach(updateBy("CriteriaEntityField"))
